//
//  WACompleteProgressView.m
//  MovieLesson
//
//  Created by luqiuan on 2018/10/27.
//  Copyright © 2018 kingsunsoft. All rights reserved.
//

#define degreesToRadians(x) (M_PI*(x)/180.0)
#define PROGRESS_LINE_WIDTH 12 //进度条宽度

#import "WACompleteProgressView.h"
@interface WACompleteProgressView ()
@property (nonatomic, strong) UILabel *titleLB;
@property (nonatomic, strong) UILabel *countLabel;
@end

@implementation WACompleteProgressView{
    CAShapeLayer *_trackLayer;
    CAShapeLayer *_progressLayer;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self drawProgressView];
        
        CGFloat score_W = self.width - 15;
        self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width * 0.6, 20)];
        [self.countLabel setCenterX:self.width/2];
        [self.countLabel setCenterY:self.height/2 - 10];
        self.countLabel.font = [UIFont systemFontOfSize:15];
        self.countLabel.textColor = [UIColor whiteColor];
        self.countLabel.text = @"1009";
        self.countLabel.textAlignment = NSTextAlignmentCenter;
        self.countLabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview:self.countLabel];
        
        self.titleLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width * 0.6, 20)];
        [self.titleLB setCenterX:self.width/2];
        [self.titleLB setCenterY:self.height/2 + 23];
        self.titleLB.font = [UIFont systemFontOfSize:16];
        self.titleLB.textColor = [UIColor whiteColor];
        self.titleLB.text = @"今日步数";
        self.titleLB.textAlignment = NSTextAlignmentCenter;
        self.titleLB.adjustsFontSizeToFitWidth = YES;
        [self addSubview:self.titleLB];
        
    }
    return self;
}

- (void)drawProgressView{
    CGFloat radius = self.width / 2 - 10;
    //画一个圆
    UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width/2, self.height/2) radius:radius startAngle:degreesToRadians(-90) endAngle:degreesToRadians(270) clockwise:YES];
    
    //进度遮罩
    _trackLayer=[CAShapeLayer layer];
    _trackLayer.frame=self.bounds;
    _trackLayer.fillColor=[[UIColor clearColor] CGColor];
    _trackLayer.strokeColor=[UIColor redColor].CGColor;//这个一定不能用clearColor，然显示不出来
    _trackLayer.lineCap=kCALineCapRound;
    _trackLayer.lineWidth=PROGRESS_LINE_WIDTH;
    _trackLayer.path=[path CGPath];
    _trackLayer.strokeEnd=1;
    
    //渐变层创建
    CALayer *gradientLayer=[CALayer layer];
    CAGradientLayer *gradientLayer1=[CAGradientLayer layer];
    gradientLayer1.frame=CGRectMake(0, 0, self.bounds.size.width/2,  self.bounds.size.height);
    [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)[UIColor yellowColor].CGColor,(id)[UIColor colorWithHexString:@"ff6c65"].CGColor, nil]];
    //注意，要把这行注释掉，不然进度条位置偏移 显示不出来）
    [gradientLayer1 setLocations:@[@0,@1]];
    [gradientLayer1 setStartPoint:CGPointMake(0.5, 0)];
    [gradientLayer1 setEndPoint:CGPointMake(0.5, 0.8)];
    [gradientLayer addSublayer:gradientLayer1];
    
    CAGradientLayer *gradientLayer2=[CAGradientLayer layer];
    //如果只需要两种颜色渐变，把下边的gradientLayer2 相关代码去掉就行
    gradientLayer2.frame=CGRectMake(self.bounds.size.width/2, 0,self.bounds.size.width/2 , self.bounds.size.height);
    [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"ff606f"].CGColor,(id)[UIColor colorWithHexString:@"ff6c65"].CGColor, nil]];
    //注意，要把这行注释掉，不然进度条位置偏移 显示不出来
    [gradientLayer2 setLocations:@[@0,@1]];
    [gradientLayer2 setStartPoint:CGPointMake(0.5, 0)];
    [gradientLayer2 setEndPoint:CGPointMake(0.5, 0.8)];
    [gradientLayer addSublayer:gradientLayer2];
    [self.layer addSublayer:gradientLayer];
    [gradientLayer setMask:_trackLayer];
    
    
    //最上层遮罩  用于动画
    _progressLayer=[CAShapeLayer layer];
    _progressLayer.frame=self.bounds;
    _progressLayer.fillColor=[UIColor clearColor].CGColor;
    _progressLayer.strokeColor=[UIColor grayColor].CGColor;
    _progressLayer.opacity=1;//背景圆环的背景透明度
    _progressLayer.lineCap=kCALineCapSquare;//这个参数主要是调整环型进度条边上是不圆角，主要有三个参数kCALineCapRound(圆角)，kCALineCapButt（直角），kCALineCapSquare（这个参数设了跟直角一样）
    _progressLayer.lineWidth=PROGRESS_LINE_WIDTH;
    _progressLayer.path=[path CGPath];
    _progressLayer.strokeEnd = 1;
    [self.layer addSublayer:_progressLayer];
}

//设置进度条的百分比 这里改成CGFloat更合理些 楼主用的NSInteger

- (void)configWith:(NSUInteger)total complete:(NSUInteger)complete{
    if (complete > 4000) {
        complete = 4000;
    }
    self.countLabel.text = [NSString stringWithFormat:@"%zd",complete];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setPercent:(CGFloat)((CGFloat)complete/(CGFloat)total) animated:YES];
    });
    
}

-(void)setPercent:(CGFloat)percent animated:(BOOL)animated{
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1.2];
    _progressLayer.strokeEnd = 1 - percent;
    [CATransaction commit];
}
@end
